import * as equipmentService from '../../services/equipment';
import {queryAllRoomList} from '../../services/tRoom';
import { qryDepts } from '../../services/department';

export default {
  namespace: 'arrival',
  state: {
    current: 1,
    currentSize: 10,
    total: 0, 
    presetId:'',
    list: [],
    roomList:[],
    detailList:[],   
    modalDetailVisible:false,
    detailModalType : 'create',
    detailInfoModalType : 'createDetail',
    modalDetailInfoVisible:false,
    modalViewVisible: false,
    currentInfoItem:{},
    currentItem:{},
    roomType:1,
    newKey:'',
    newKey1:'',
    newKey2:'',
    deptList: [],
   
  },
  reducers: {      
    queryPresetListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },  
  
    queryDeptsSuc(state, { payload: deptList }) {
      return { ...state, deptList };
    },
    showDetailModal (state, { payload }) {
        return { ...state, ...payload,  modalDetailVisible: true }
    },
    hideDetailModal (state) {
        return { ...state, modalDetailVisible: false,  }
    },
    showDetailInfoModal (state, { payload }) {
        return { ...state, ...payload, newKey2 : new Date().getTime() + '' , modalDetailInfoVisible: true }
    },
    hideDetailInfoModal (state) {
        return { ...state, modalDetailInfoVisible: false, newKey2 : new Date().getTime() + '' }
    },
    showInfoModal (state, { payload }) {
        return { ...state, ...payload, newKey1 : new Date().getTime() + '' , modalViewVisible: true }
    },
    hideInfoModal (state) {
        return { ...state, modalViewVisible: false, newKey1 : new Date().getTime() + '' }
    },      
   
  },
  effects: {    
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryAllRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
    *queryPreset({ payload },{ call, put }) {
      const { body } = yield call(equipmentService.queryPresetList,payload);
      yield put({ type: 'queryPresetListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords, datacenterId:payload.datacenterId }});
    },
    
    *qryDetailById({ payload },{ call, put }){
      const { body } = yield call(equipmentService.queryDetailList,payload);
      yield put({
          type: 'showDetailModal',
          payload: {
            currentItem: body.obj,
            presetId:body.obj.id_,
            detailList: body.data,
            detailModalType: 'edit',
            title: '查看清单详情',
          },
      });
    },

   *viewDetail({ payload },{ call, put }){
      const { body } = yield call(equipmentService.queryDetailList,payload);
      yield put({
          type: 'showInfoModal',
          payload: {
            currentItem: body.obj,
            detailList: body.data,
          },
      });
    },

   *doCheck({ payload },{ call, put }) {   
      const { body }  = yield call(equipmentService.doCheck,payload.data);
      if(body.httpCode==200){
         yield put({ type: 'qryDetailById', payload:payload.presetId});
         yield put({ type: 'hideDetailInfoModal', payload});   
      }        
    },

   *qryDetailInfoById({ payload },{ call, put }){ 
      const { body } = yield call(equipmentService.qryDetailInfoById,payload);
      yield put({
          type: 'showDetailInfoModal',
          payload: {
            currentInfoItem: body.data,
            roomType: body.data.roomType,
            detailInfoModalType: 'editDetail',
          },
      });      
      yield put({ type: 'queryDepts'});  
    },

    *queryDepts({ payload }, { call, put }) {
      const { body } = yield call(qryDepts);
      yield put({ type: 'queryDeptsSuc', payload: body.data });
    },
    
    *reload({ payload },{ call, put }) {
      yield put({ type: 'queryPreset', payload});
      yield put({ type: 'hideDetailModal'});
    },
    
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/equipment/arrival') {
          dispatch({ type: 'queryRoom'});
          dispatch({ type: 'queryPreset', payload:{}});
        }
      });
    },
  },
};
